package com.LostAndFound.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.LostAndFound.utils.DataSourceUtils;

public class FalseDao {

	public int getTotalFalse(String cid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		// invalidfalg=4 && foundflag=4 表示虚假欺诈记录
		String sql = "select count(*) from article where invalidflag = 4 and foundflag = 4 ";
		Long total = 0L;
		// 拼接分类
		if("all".equals(cid)) {
			sql = sql+" ";
			total = (Long) runner.query(sql, new ScalarHandler());
		}else if(!"all".equals(cid)) {
			sql = sql + " and cid = ? ";
			total = (Long) runner.query(sql, new ScalarHandler(),cid);
		}
		return total.intValue();
	}

	public List<Map<String, Object>> getFlaseRecordList(int startIndex, int pageSize, String cid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select a.*,u.*,c.* from article a,user u,category c "
				+ " where foundflag = 4 and invalidflag = 4 "
				+ " and a.uid=u.uid and a.cid = c.cid ";
		List<Map<String, Object>> mapList = null;
		// 拼接分类
		if("all".equals(cid)) {
			sql = sql+" order by a.issuetime desc limit ?,? ";
			mapList = runner.query(sql, new MapListHandler(),startIndex,pageSize);
		}else if(!"all".equals(cid)) {
			sql = sql + " and a.cid = ? order by a.issuetime desc limit ?,? ";
			mapList = runner.query(sql, new MapListHandler(),cid,startIndex,pageSize);
		}
		return mapList;
	}

	// delete false record
	public void handleDeleteFlaseRecord(String aid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "delete from article where aid = ?";
		runner.update(sql,aid);
	}

}
